// miniprogram/components/scorecard/scorecard.js
const app = getApp()
 Component({

  properties:{
    isplayerscorecard:{
        type:Boolean,
        vaule: true
    },
    isdisplayertitle:{
      type:Boolean,
      vaule: true,
      observer: function (newVal, oldVal) {
        
        this._isdisplayertitlecardpropertyChange(newVal,oldVal);
   
     } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    
  },
    isteamscorecard:{
      type:Boolean,
      vaule: false
    },
    baseimgaeUrl:{
      type:String,
      value:""
    },
    Pplayerscorecard:{
      type: Object,
      value:undefined,
      observer: function (newVal, oldVal) {
        
        this._playerscorecardpropertyChange(newVal,oldVal);
   
     } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    
    },

    teamscorecard:{
      type: Object,
      value:undefined,
      observer: function (newVal, oldVal) {
        
        this._teamscorecardpropertyChange(newVal,oldVal);
   
     } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    
    },

    PCourseYardCard:{
      type: Object,
      value:undefined,
      observer: function (newVal, oldVal) {
        
        this._CourseYardCardpropertyChange(newVal,oldVal);
   
     } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    
    },
    matchinfo:{
      type: Object,
      value:undefined,
      observer: function (newVal, oldVal) {
        
        this._matchinfocardpropertyChange(newVal,oldVal);
   
     } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    isplayerscorecard:true,
    isteamscorecard:false,
    currentTab:0,
    currentpageheight:0,
    isdisplayertitle:true,
    playerscorecard:[],
    isallopen:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 

  methods:{

    _refreshscorecard(scorecard){
      var that = this;
      that.setData
      ({  
       
        playerscorecard:scorecard
      });
      console.log(that.data.playerscorecard);
    },

    _refreshCourseYardCard(CourseYardCard){
      var that = this;
      that.setData
      ({  
        CourseYardCard :CourseYardCard,
      });
      console.log(that.data.CourseYardCard);
    },

    _isdisplayertitlecardpropertyChange:function(newVal,oldVal){
     
      this.setData({
        isdisplayertitle:newVal
        });
    },

    _CourseYardCardpropertyChange:function(newVal,oldVal){
      var that = this;
      that.setData
      ({  CourseYardCard :newVal});
    },

    _teamscorecardpropertyChange:function(newVal,oldVal){
      var that = this;
      console.log('teamscorecard',newVal);
        var index = 0;
      newVal.playerscorecards.forEach(round => {
        var roundindex= 0 ;
        newVal.playerscorecards[index].playerscorecard.forEach(item=>{
          newVal.playerscorecards[index].playerscorecard[roundindex].isallopen = false;
          roundindex++;
        })
        index++;
      })
      console.log('teamscorecard',newVal);
      that.setData
      ({  teamscorecard :newVal});
    },

    _playerscorecardpropertyChange:function(newVal,oldVal){
      console.log("scorecard-newVal");
      console.log(newVal);
      var that = this;
      newVal.scorecards.forEach(scorecard=>{
        var scorestotals = 0;
        scorecard.scoreperformans.forEach(item=>{
          scorestotals+= item.value;
        })
        scorecard.thru = scorestotals;
      })
      console.log(newVal);
      that.setData
      ({playerscorecard:newVal});
      
      that.fillheight();
    },
    _matchinfocardpropertyChange:function(newVal,oldVal){
      console.log("newVal");
      console.log(newVal);
      var that = this;
      var jsonmatchinfoobj = newVal;
      that.setData({
        currentTab:jsonmatchinfoobj.currentround-1
        });
        console.log(this.data.currentTab);
        that.fillheight();
    },
    

    fillheight(){
          var that = this;   
          //填充所有的swiperg高度
          console.log("#v_scorecard_contain"+that.data.currentTab);
          var obj = that.createSelectorQuery().select("#v_scorecard_contain"+that.data.currentTab);
          console.log(obj);
          obj.boundingClientRect(function(rect){
            if (rect == null || rect.height == null) {
              return;
            }
            console.log(rect);
           that.setData({
            currentpageheight:rect.height + 50 // 节点的高度
           }
           )
          }).exec(); 
   },

     //  tab切换逻辑
   swichNav: function( e ) {
    var that = this;
    
    if( that.data.currentTab === e.target.dataset.current ) {
        return false;
    } else {
        that.setData( {
            currentTab: e.target.dataset.current
        })
    }
    that.fillheight();
   },

    bindChange: function( e ) {
    var that = this;
    that.setData( { currentTab: e.detail.current });
    that.fillheight();
  },
      
  _openplayerinfo:function(e){
      var playerid= e.currentTarget.dataset.playerid;
      wx.navigateTo({
        url: '/pages/Players/playerinfo/index?selectid='+playerid,
      })
  },

  openmore:function(e){
      
      let{teamscorecard,currentTab} = this.data;
      var playerscorecards =  teamscorecard.playerscorecards[currentTab];
      if( e.currentTarget.dataset.index>=0){
          const{ index} = e.currentTarget.dataset;
          playerscorecards.playerscorecard[index].isallopen = !playerscorecards.playerscorecard[index].isallopen;
      }
      else{
        this.setData({isallopen:!this.data.isallopen})
        var index=0;
        playerscorecards.playerscorecard.forEach(item=>{
          playerscorecards.playerscorecard[index].isallopen = this.data.isallopen;
          index++;
        });
        
      }
      teamscorecard.playerscorecards[currentTab] = playerscorecards;
      this.setData({teamscorecard});
      console.log(teamscorecard);
      this.fillheight();
  }

  }
})